﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAFS05ADD : DocumentBase
    {
        #region-------------Properties-----------------------
        DataRow LastRow = null;
        DataSet SetMap = null;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        YCAFS05ADDData m_Data, m_DataX;
        public DataSet dsParamaters;
        YCAFS05ADDReport m_rpt;
        DataTable tbUser = null;
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB SetMaster
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetMaster");
            }
        }

        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        CollectionViewSource vwABPOS_MAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPOS_MAT");
            }
        }
        CollectionViewSource vwEKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS");
            }
        }

        CollectionViewSource vwYARNMatrix
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwYARNMatrix");
            }
        }
        CollectionViewSource vwFABMatrix
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFABMatrix");
            }
        }
        CollectionViewSource vwFSSTAMM
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFSSTAMM");
            }
        }

        DataRow dwFSSTAMM
        {
            get
            {
                if (vwFSSTAMM.View == null)
                    return null;
                if ((DataRowView)vwFSSTAMM.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFSSTAMM.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwSFSGROE
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwSFSGROE");
            }
        }
        DataRow dwSFSGROE
        {
            get
            {
                if (vwSFSGROE.View == null)
                    return null;
                if ((DataRowView)vwSFSGROE.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwSFSGROE.View.CurrentItem).Row;
            }
        }

        CollectionViewSource vwSFSFARBEN
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwSFSFARBEN");
            }
        }
        DataRow dwSFSFARBEN
        {
            get
            {
                if (vwSFSFARBEN.View == null)
                    return null;
                if ((DataRowView)vwSFSFARBEN.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwSFSFARBEN.View.CurrentItem).Row;
            }
        }

        CollectionViewSource vwSFSSTAMM
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwSFSSTAMM");
            }
        }
        DataRow dwSFSSTAMM
        {
            get
            {
                if (vwSFSSTAMM.View == null)
                    return null;
                if ((DataRowView)vwSFSSTAMM.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwSFSSTAMM.View.CurrentItem).Row;
            }
        }

        CollectionViewSource vwABPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPOS");
            }
        }
        DataRow dwABPOS
        {
            get
            {
                if (vwABPOS.View == null)
                    return null;
                if ((DataRowView)vwABPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwFDECKMAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFDECKMAT");
            }
        }
        DataRow dwFDECKMAT
        {
            get
            {
                if (vwFDECKMAT.View == null)
                    return null;
                if ((DataRowView)vwFDECKMAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFDECKMAT.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWDECKMAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwWDECKMAT");
            }
        }
        DataRow dwWDECKMAT
        {
            get
            {
                if (vwWDECKMAT.View == null)
                    return null;
                if ((DataRowView)vwWDECKMAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWDECKMAT.View.CurrentItem).Row;
            }
        }


        DataRow dwEKPOS
        {
            get
            {
                if (vwEKPOS.View == null)
                    return null;
                if ((DataRowView)vwEKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKPOS.View.CurrentItem).Row;
            }
        }

        CollectionViewSource vwABKOPF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABKOPF");
            }
        }

        DataRow dwABKOPF
        {
            get
            {
                if (vwABKOPF.View == null)
                    return null;
                if ((DataRowView)vwABKOPF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABKOPF.View.CurrentItem).Row;
            }
        }

        CollectionViewSource vwLIEFDAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwLIEFDAT");
            }
        }
        DataRow dwLIEFDAT
        {
            get
            {
                if (vwLIEFDAT.View == null)
                    return null;
                if ((DataRowView)vwLIEFDAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwLIEFDAT.View.CurrentItem).Row;
            }
        }
        DataRow dwBOM = null;
        string ART
        {
            get
            {
                return m_Data.Left(txtMAT.Text.ToString() + "                      ", 20);
            }
        }

        string user = "";
        #endregion
        public YCAFS05ADD()
        {
            InitializeComponent();
            this.Name = "插入新原料";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            tbUser = (DataTable)dicParamater["USERRIGHT"];
            m_Data = new YCAFS05ADDData(OracleDB, proxy);
            user = (string)dicParamater["User"];
            txtMAT.Text = "B08F3BK001          ";
            txtFSART.Text = "2411309005";
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                vwFSSTAMM.View.CurrentChanged += vwFSSTAMM_CurrentChanged;
                ResetSetInterface();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        void vwFSSTAMM_CurrentChanged(object sender, EventArgs e)
        {
            if (LastRow == dwFSSTAMM)
                return;
            LastRow = dwFSSTAMM;
            using (new LongOperation(this, "数据更新中"))
            {
                DataSet SResult = m_Data.GetData(dwFSSTAMM);
                SetMaster.Clear();
                SetMaster.Merge(SResult);
                SetProd.ABPOS.Clear();
                SetProd.Merge(SResult.Tables["ABPOS"]);
                if (vwABPOS.View != null)
                {
                    vwABPOS.View.MoveCurrentToFirst();
                }
            }
        }

        private void btnQuerySData_Click(object sender, EventArgs e)
        {
            try
            {
                //获得子物料信息
                DataRow dwInput = null;
                string mtype = "T";
                switch (cbWTYE.Text)
                {
                    case "面料":
                        dwInput = SetProd.WASTAMM.NewRow();
                        dwInput["WAMANDANT"] = "00";
                        dwInput["WAART"] = ART;
                        mtype = "M";
                        break;
                    case "高级辅料":
                        dwInput = SetProd.FSSTAMM.NewRow();
                        dwInput["FSMANDANT"] = "00";
                        dwInput["FSART"] = ART;
                        mtype = "T";
                        break;
                    default:
                        break;
                }

                DataSet SResult = new DataSet();
                using (new LongOperation(this, "数据更新中"))
                {
                    SResult = m_Data.GetSData(dwInput, mtype);
                    SetProd.Clear();
                    SetProd.Merge(SResult);
                }
                if (vwSFSSTAMM.View != null)
                {
                    vwSFSSTAMM.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

            DataSet setRpt = new DataSet();

            if (setRpt == null)
                return;
            m_DataX = new YCAFS05ADDData(dicParamater);
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            if (m_DataX.exAgent._app == null)
            {
                m_DataX.exAgent = new ExcelX.ExcelLib.ExcelAgent(dicParamater);
            }
            m_rpt = new YCAFS05ADDReport(dicParamater);
            m_rpt.dsSource = SetProd;
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL");
        }
        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            DataRow dwTemp = SetData.FSSTAMM.Rows.Add();
            dwTemp["FSMANDANT"] = "00";
            dwTemp["FSART"] = txtFSART.Text.Trim();

            SetProd.AcceptChanges();
            DataSet SResult = m_Data.GetFSList(dwTemp);
            SetData.Clear();
            SetData.Merge(SResult);
            if (vwFSSTAMM.View != null)
            {
                vwFSSTAMM.View.MoveCurrentToFirst();
            }
        }
        public void ResetSetInterface()
        {
            SetMap = new SETINTEXDB();
            DataSet setResult = new DataSet();
            string filePath = Path.Combine(PathHelper.ApplicationPath, "Report\\Interface\\FSCHGMAPINTEX.XML");
            setResult.ReadXml(filePath);
            SetMap.Merge(setResult);
            m_Data.SetCAD = SetMap;
            m_Data.SetExt = new DataSet();
        }
        private void btnAppend_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                txtMessage.Text = "";
                DataTable tbBOM = null;
                if (!SetProd.Tables.Contains("FSSTUELI_EXT"))
                {
                    tbBOM = SetProd.FSSTUELI.Clone();
                    tbBOM.TableName = "FSSTUELI_EXT";
                    SetProd.Merge(tbBOM);
                }
                tbBOM = SetProd.Tables["FSSTUELI_EXT"];
                DataRow dwData = tbBOM.NewRow();

                dwData["FSSAEND"] = m_Data.date;
                dwData["FSSNEUD"] = m_Data.date;
                dwData["FSSART"] = dwFSSTAMM["FSART"];
                string pos = Helper.GetMaxRowNum(SetMaster.FSSTUELI, "FSSART", dwFSSTAMM["FSART"].ToString(), "FSSPOS");
                string pos1 = Helper.GetMaxRowNum(SetProd.Tables["FSSTUELI_EXT"], "FSSART", dwFSSTAMM["FSART"].ToString(), "FSSPOS");
                if (System.Convert.ToInt64(pos1) > System.Convert.ToInt64(pos))
                {
                    pos = pos1;
                }
                dwData["FSSPOS"] = pos;
                dwData["FSSVAR"] = dwFSSTAMM["FSVAR"];
                dwData["FSSFARBE"] = Helper.Left(cbFFARBE.Text + "                  ", 10);
                dwData["FSSGROE"] = Helper.Left(cbFSIZE.Text + "                  ", 7);

                dwData["FSSMATKZ"] = "T";
                if (cbWTYE.Text.ToString().Trim() == "面料")
                {
                    dwData["FSSMATKZ"] = "F";
                }
                dwData["FSSMART"] = dwSFSSTAMM["FSART"];
                dwData["FSSMVAR"] = dwSFSSTAMM["FSVAR"];
                dwData["FSSPOSTYP"] = "M";
                dwData["FSSMFARBE"] = Helper.Left(cbSFFARBE.Text + "                  ", 10);
                dwData["FSSMGROE"] = Helper.Left(cbSFSIZE.Text + "                  ", 7);
                dwData["FSSVERBR"] = System.Convert.ToInt64(txtSQTY.Text.ToString()) * 1000;
                dwData["FSSVERL"] = System.Convert.ToInt64(txtSWASTAGE.Text.ToString()) * 100;
                string um = cbSMEEH.Text.Trim();
                dwData["FSSVBME"] = Helper.Left(um + "    ", 3);
                dwData["FSSVBFKT"] = "000001000000";
                dwData["FSSVBGRP"] = "1";
                if (um == "M")
                {
                    dwData["FSSVBGRP"] = "2";
                }
                if (um == "KG")
                {
                    dwData["FSSVBGRP"] = "4";
                }
                tbBOM.Rows.Add(dwData);
                tbBOM.AcceptChanges();

                DataSet SResult = m_Data.Convert2FSSTUELI(dwData);
                foreach (DataRow rw in SResult.Tables[0].Rows)
                {
                    m_Data.INTEXDBC.SaveFSSTUELI(rw);
                    dwBOM = rw;
                    txtMessage.Text = dwBOM["FSSART"].ToString() + dwBOM["FSSPOS"].ToString() + "已经插入新的原料!";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnRefreshAB_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                DataSet SResult = null;
                if (dwABPOS == null)
                {
                    MessageBox.Show("请选择生产单！");
                    return;
                }
                txtMessage.Text = "";
                DataTable tbBOM = null;
                string art = "";
                string var = "";
                string color = "";
                string size = "";
                tbBOM = SetProd.Tables["FSSTUELI_EXT"];
                var qAB = from DataRow row in SetProd.ABPOS.AsEnumerable()
                          where row["ABPAUFKRS"].ToString() == dwABPOS["ABPAUFKRS"].ToString()
                          && row["ABPAUFLFD"].ToString() == dwABPOS["ABPAUFLFD"].ToString()
                          select row;
                foreach (DataRow dwABPOS_EXT in qAB)
                {
                    art = dwABPOS_EXT["ABPART"].ToString();
                    var = dwABPOS_EXT["ABPVAR"].ToString();
                    color = dwABPOS_EXT["ABPFARBE"].ToString();
                    size = dwABPOS_EXT["ABPGROE"].ToString();
                    foreach (DataRow rm in tbBOM.Rows)
                    {
                        //FSSVAR	FSSFARBE	FSSGROE	FSSAUFM	FSSVERP
                        //FSSMATKZ FSSMART	FSSMVAR	FSSMFARBE FSSMGROE	 	FSSVERBR FSSVBME	FSSVBGRP	FSSVBFKT
                        //FSSVBKZ==G 用量和尺码相关
                        string FSSVAR = rm["FSSVAR"].ToString();
                        string FSSFARBE = rm["FSSFARBE"].ToString().Trim();
                        string FSSGROE = rm["FSSGROE"].ToString();
                        string FSSAUFM = rm["FSSAUFM"].ToString();
                        string FSSVERP = rm["FSSVERP"].ToString();
                        string FSSPOSTYP = rm["FSSPOSTYP"].ToString();
                        //  if (FSSVAR.Trim() != var)
                        //   continue;

                        //============ 
                        string FSSPOS = rm["FSSPOS"].ToString();
                        string FSSMATKZ = rm["FSSMATKZ"].ToString();
                        string FSSMART = rm["FSSMART"].ToString();
                        string FSSMVAR = rm["FSSMVAR"].ToString();
                        string FSSMFARBE = rm["FSSMFARBE"].ToString();
                        string FSSMGROE = rm["FSSMGROE"].ToString();
                        string FSSVERBR = rm["FSSVERBR"].ToString();
                        string FSSVBME = rm["FSSVBME"].ToString();
                        string FSSVBFKT = rm["FSSVBFKT"].ToString();
                        string FSSVERL = rm["FSSVERL"].ToString();
                        FSSVERBR = (Convert.ToDouble(FSSVERBR) * Convert.ToDouble(FSSVBFKT) * 0.000001 * (1 + Convert.ToDouble(FSSVERL) * 0.0001)).ToString();
                        rm["FSSVERBR"] = FSSVERBR;
                        //先处理颜色
                        EventDispatcher.Instance.RaiseStatus("处理原料需求" + FSSMART + FSSMVAR + FSSMFARBE, StatusEventType.RunningProgress);
                        DataRow rtp = null;
                        if (m_Data.SetExt.Tables.Contains("FSSTUELI_EXT"))
                        {
                            m_Data.SetExt.Tables["FSSTUELI_EXT"].Clear();
                        }
                        DataTable tbFS05EXT = tbBOM.Clone();
                        DataRow rFS05 = tbFS05EXT.NewRow();
                        rFS05.ItemArray = rm.ItemArray;
                        if (FSSFARBE.Trim() == "*")
                        {
                            rFS05["FSSMFARBE"] = color;
                        }
                        tbFS05EXT.Rows.Add(rFS05);
                        m_Data.SetExt.Merge(tbFS05EXT);

                        if (FSSFARBE.Trim() != "")
                        {
                            if (FSSFARBE.Trim() != "*")
                            {
                                if (FSSFARBE.Trim() != color.Trim())
                                    continue;
                            }
                        }
                        if (FSSGROE.Trim() != "")
                        {
                            if (FSSGROE.Trim() != "*")
                            {
                                if (FSSGROE.Trim() != size.Trim())
                                    continue;
                            }
                        }
                        switch (FSSMATKZ)
                        {
                            case "F"://面料 
                                SResult = m_Data.Convert2WDECK(dwABPOS_EXT);
                                foreach (DataRow rw in SResult.Tables[0].Rows)
                                {
                                    m_Data.INTEXDBC.SaveWDECK(rw);
                                }
                                break;
                            case "T":
                                SResult = m_Data.Convert2FDECKB(dwABPOS_EXT);
                                foreach (DataRow rw in SResult.Tables[0].Rows)
                                {
                                    m_Data.INTEXDBC.SaveFDECK(rw);
                                    txtMessage.Text = rw["FDAUFKRS"].ToString() + rw["FDAUFLFD"].ToString() + rw["FDAUFLFD"].ToString() + "已经插入新的原料需求!";
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnUpdateIntex_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                using (new LongOperation(this, "数据更新中"))
                {

                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
